<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
<script>
  /*闫清倩*/
  function binarySearch(arr, target) {
    let lowIndex = 0;
    let count = 0; // 次数
    let heightIndex = arr.length - 1;
    while (lowIndex <= heightIndex) {
      let midIndex = Math.floor((lowIndex + heightIndex) / 2);
      count++;
      if (target < arr[midIndex]) {
        heightIndex = midIndex - 1;
      } else if (target > arr[midIndex]) {
        lowIndex = midIndex + 1;
      } else {
        return [midIndex, count];
      }
    }
    return [-1, count];
  }

  let arr = [];
  for (let i = 0; i < 100; i++) {
    arr.push(i + 1);
  }
  console.log(binarySearch(arr, 33));

</script>
</body>
</html>
